library(lavaan)

##Prepare data with sufficient statisitics##
mymeans<-matrix(c(3.06893, 2.92590, 3.11013), ncol=3,nrow=1)
mysd<-c(0.84194,0.88934,0.83470)
mat <- c(1.00000,
         0.55226, 1.00000,
         0.56256, 0.66307, 1.00000)
mycor <- getCov(mat, lower = TRUE)
##Transform correlation matrix to covariance matrix using information above##
myvarcov <- outer(mysd, mysd, FUN="*")
mycov <- mycor * myvarcov

rownames(mycor) <-c( "Glad", "Cheerful", "Happy")
colnames(mycor) <-c( "Glad", "Cheerful", "Happy")

rownames(mycov) <-c( "Glad", "Cheerful", "Happy")
colnames(mycov) <-c( "Glad", "Cheerful", "Happy")
mynob<-823
cr <- ztable(mycor, zebra = 2)
tab <- data.frame(Mean = round(mymeans[1, ], 3), SD = round(mysd, 3), 
                  Var = round(mysd*mysd, 3))

pt <- t(cbind(mycor, tab))
ztable(pt, zebra = 2, caption = "Descriptive Statistics")
Descriptive Statistics
  Glad Cheerful Happy
Glad 1.00 0.55 0.56
Cheerful 0.55 1.00 0.66
Happy 0.56 0.66 1.00
Mean 3.07 2.93 3.11
SD 0.84 0.89 0.83
Var 0.71 0.79 0.70

Fundamental SEM equation

\[ \Sigma = \Lambda \Psi \Lambda + \Theta \tag{1} \]

Latent Cheer with one indicator

# Mplus file
l.cheer.inp
grViz("
digraph Cheer {

node [shape = circle]
Positive;

node [shape = box]
Cheer;

# Edges

Positive -> Cheer [label = <&lambda;>];
Positive:n -> Positive:n [dir=both, label = <&psi;>,position = N]
Cheer:s -> Cheer:s [dir=both, label = <&theta;>]
}  
")

Measurement Model: 3 indicators

using correlations only (instead of variance/covariance matirx)

mod1<-'Positive =~ 1*Cheerful
Positive~~Positive
Cheerful~~0*Cheerful'
#Save output to fit1##
fit1<-cfa(mod1, sample.cov=mycov, sample.nobs = mynob, sample.mean=mymeans, 
          std.lv=F)
## Found more than one class "Model" in cache; using the first, from namespace 'MatrixModels'
##Request for summary of output##
summary(fit1, fit.measures=T)
## lavaan (0.5-20) converged normally after   9 iterations
## 
##   Number of observations                           823
## 
##   Estimator                                         ML
##   Minimum Function Test Statistic                0.000
##   Degrees of freedom                                 0
## 
## Model test baseline model:
## 
##   Minimum Function Test Statistic                0.000
##   Degrees of freedom                                 0
##   P-value                                           NA
## 
## User model versus baseline model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1070.768
##   Loglikelihood unrestricted model (H1)      -1070.768
## 
##   Number of free parameters                          1
##   Akaike (AIC)                                2143.536
##   Bayesian (BIC)                              2148.249
##   Sample-size adjusted Bayesian (BIC)         2145.074
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent Confidence Interval          0.000  0.000
##   P-value RMSEA <= 0.05                          1.000
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Information                                 Expected
##   Standard Errors                             Standard
## 
## Latent Variables:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##   Positive =~                                         
##     Cheerful          1.000                           
## 
## Variances:
##                    Estimate  Std.Err  Z-value  P(>|z|)
##     Positive          0.790    0.039   20.285    0.000
##     Cheerful          0.000
grViz("
    
digraph CFA {

node [shape = circle]
Positive;

node [shape = box]
Glad; Cheer; Happy;

# Edges
Positive -> Glad [label = <&lambda;<sub>1</sub>>];
Positive -> Cheer [label = <&lambda;<sub>2</sub>>];
Positive -> Happy [label = <&lambda;<sub>3</sub>>];
Positive:n -> Positive:n [dir=both, label = <&psi;>]
Glad:s -> Glad:s [dir=both, label = <&theta;<sub>1</sub>>]
Cheer:s -> Cheer:s [dir=both, label = <&theta;<sub>2</sub>>]
Happy:s -> Happy:s [dir=both, label = <&theta;<sub>3</sub>>]

    {rank = same; Positive;}
    {rank = same; Glad; Cheer; Happy;}
}  
")